package pvz;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import org.standard.Utility;

import pvz.a.*;
import pvz.b.ZombieImage;

public class PVZMainCanvas extends Panel implements Runnable, MouseListener,
		MouseMotionListener {
	private Image screen;
	private Graphics2D graphics;
	private BackGroundImage bkimg;
	private TopImage topimg;
	private PlantImage dragplant = null;
	private Point mp = new Point();
	private ArrayList<PlantImage> plants = new ArrayList<PlantImage>();
	private ArrayList<ZombieImage> zombies = new ArrayList<ZombieImage>();
	private long gametime;

	public PVZMainCanvas() {
		this.bkimg = new BackGroundImage(0, 0);
		this.screen = Utility.createImage(bkimg.getWidth(), bkimg.getHeight(),
				true);
		this.setPreferredSize(new Dimension(bkimg.getWidth(), bkimg
						.getHeight()));
		this.graphics = (Graphics2D) screen.getGraphics();
		this.topimg = new TopImage(100, 0);
		topimg.addCard("Chomper", 0, 20 * 1000);
		topimg.addCard("Repeater", 0, 30 * 1000);
		ZombieImage bz = new ZombieImage("BucketheadZombie", 1200, 100);
		bz.setSpeed(2);
		ZombieImage cz = new ZombieImage("ConeheadZombie", 1200, 260);
		cz.setSpeed(3);
		zombies.add(bz);
		zombies.add(cz);
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
		this.MainLoop();
	}

	private void MainLoop() {
		Thread mainloop = new Thread(this);
		mainloop.start();
	}

	private void drawpic(long time) {
		graphics.drawImage(bkimg.getImage(), bkimg.getPosX(), bkimg.getPosY(),
				null);
		topimg.redraw(time);
		graphics.drawImage(topimg.getImage(), topimg.getPosX(), topimg
				.getPosY(), null);
		if (dragplant != null) {
			graphics.drawImage(dragplant.getImage(), mp.getX()
					- dragplant.getWidth() / 2, mp.getY()
					- dragplant.getHeight() / 2, null);
		}
		for (PlantImage plant : plants) {
			graphics.drawImage(plant.getImage(), plant.getPosX(), plant
					.getPosY(), null);
		}
		for (int i = 0; i < zombies.size(); i++) {
			ZombieImage zombie = zombies.get(i);
			int flag = 0;
			for (PlantImage plant : plants) {
				if (plant.getPosX() + plant.getWidth() >= zombie.getPosX()
						&& (plant.getPosY() + plant.getHeight() - 90) / 88 * 88 + 90 == (zombie
								.getPosY()
								+ zombie.getHealth() - 90) / 88 * 88 + 90) {
					flag = 1;
				}
			}
			if (flag == 0) {
				graphics.drawImage(zombie.getWalkimg(), zombie.getPosX(),
						zombie.getPosY(), null);
				zombie.setPosX(zombie.getPosX() - zombie.getSpeed() * 1);
			} else {
				graphics.drawImage(zombie.getAttackimg(), zombie.getPosX(),
						zombie.getPosY(), null);
			}
		}
		this.repaint();
	}

	public void update(Graphics g) {
		paint(g);
	}

	public void paint(Graphics g) {
		g.drawImage(screen, 0, 0, null);
		g.dispose();
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		long starttime = System.currentTimeMillis();
		while (true) {
			gametime = System.currentTimeMillis() - starttime;
			drawpic(gametime);
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		ArrayList<PlantCardImage> list = topimg.getList();
		if (mp.getX() >= 175 && mp.getX() <= 700 && mp.getY() >= 0
				&& mp.getY() <= 80) {
			for (int i = 0; i < list.size(); i++) {
				PlantCardImage card = list.get(i);
				if ((mp.getX() - topimg.getPosX() >= card.getPosX())
						&& (mp.getX() - topimg.getPosX() <= card.getPosX()
								+ card.getWidth())
						&& (mp.getY() - topimg.getPosY()) >= card.getPosY()
						&& (mp.getY() - topimg.getPosY() <= card.getPosY()
								+ card.getHeight())) {
					try {
						if (card.getActive() == false) {
							card.reset(gametime);
							System.out.println(list.get(i).getActive());
							dragplant = (PlantImage) card.getPlant().clone();
						}
					} catch (CloneNotSupportedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					break;
				}
			}
		} else if (mp.getX() >= 250 && mp.getX() <= 1000 && mp.getY() >= 90
				&& mp.getY() <= 580) {
			int row = (mp.getX() - 250) / 86;
			int col = (mp.getY() - 90) / 88;
			if (dragplant != null) {
				dragplant.setPosX(row * 86 + 250);
				dragplant.setPosY(col * 88 + 90);
				plants.add(dragplant);
			}
			dragplant = null;
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
		mp.setX(arg0.getX());
		mp.setY(arg0.getY());
		System.out.println(mp.getX());
		System.out.println(mp.getY());
	}

}
